﻿Imports System.Text

Public Class frmExport4WC


    Private Sub frmExport4WC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim dt As DataTable
        Dim ofun As New Functions
        dt = ofun.DoQueryReturnDT("select cardcode, cardname from OCRD where cardtype='S' order by Cardname")
        cbVendor.DisplayMember = "Cardname"
        cbVendor.ValueMember = "CardCode"
        cbVendor.DataSource = dt
    End Sub

    Private Sub btnExport_Click(sender As System.Object, e As System.EventArgs) Handles btnExport.Click
        Try
            Dim StrQueryWCSKU As String = "exec sp_AI_SKU_Exp4Wincor '" + cbWCDate.Value.ToString("MM/dd/yyyy") + "'"
            Dim StrQueryWCSKUFull As String = "exec sp_AI_SKU_Exp4WincorFull '" + cbWCDate.Value.ToString("MM/dd/yyyy") + "'"

            Dim StrQueryWCPLU As String = "exec sp_AI_PLU_Exp4Wincor '" + cbWCDate.Value.ToString("MM/dd/yyyy") + "'"
            Dim StrQueryWCDept As String = "exec sp_AI_Dept_Exp4Wincor '" + cbWCDate.Value.ToString("MM/dd/yyyy") + "'"
            Dim StrQueryWCCate As String = "exec sp_AI_Cate_Exp4Wincor '" + cbWCDate.Value.ToString("MM/dd/yyyy") + "'"

            Dim StrQueryAppInv As String = "exec sp_AI_rpt_AppleInventory '" _
                                           + cbAppFromDate.Value.ToString("MM/dd/yyyy") + "','" _
                                           + cbAppleToDate.Value.ToString("MM/dd/yyyy") + "','" _
                                           + cbVendor.SelectedValue.ToString + "'"
            Dim StrQueryAppSales As String = "exec sp_AI_rpt_AppleSales '" _
                                             + cbAppFromDate.Value.ToString("MM/dd/yyyy") + "','" + _
                                             cbAppleToDate.Value.ToString("MM/dd/yyyy") + "','" + _
                                             CStr(cbVendor.SelectedValue) + "'"
            Dim dt As DataTable
            Dim ofn As New Functions
            If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim strFolder As String = FolderBrowserDialog1.SelectedPath
                txtFolder.Text = strFolder
                If ckWCPLU.Checked Then
                    dt = ofn.DoQueryReturnDT(StrQueryWCPLU)
                    Datatable2Text(dt, strFolder + "\PLU_" + cbWCDate.Value.ToString("yyyyMMddhhmmss") + ".txt")
                End If
                If ckWCSKU.Checked Then
                    dt = ofn.DoQueryReturnDT(StrQueryWCSKU)
                    Datatable2Text(dt, strFolder + "\SKU_" + cbWCDate.Value.ToString("yyyyMMddhhmmss") + ".txt")
                End If
                If ckWCSKUFull.Checked Then
                    dt = ofn.DoQueryReturnDT(StrQueryWCSKUFull)
                    Datatable2Text(dt, strFolder + "\SKU_" + cbWCDate.Value.ToString("yyyyMMddhhmmss") + ".txt")
                End If

                If ckWCDept.Checked Then
                    dt = ofn.DoQueryReturnDT(StrQueryWCDept)
                    Datatable2Text(dt, strFolder + "\DEPT_" + cbWCDate.Value.ToString("yyyyMMddhhmmss") + ".txt")
                End If
                If ckWCCate.Checked Then
                    dt = ofn.DoQueryReturnDT(StrQueryWCCate)
                    Datatable2Text(dt, strFolder + "\CATG_" + cbWCDate.Value.ToString("yyyyMMddhhmmss") + ".txt")
                End If

                If ckAppInv.Checked Then
                    dt = ofn.DoQueryReturnDT(StrQueryAppInv)
                    Datatable2Text(dt, strFolder + "\282787_AP_INV_" + CStr(Now.Date.ToString("yyMMdd")) + CStr("01") + ".txt")
                End If

                If ckAppSales.Checked Then
                    dt = ofn.DoQueryReturnDT(StrQueryAppSales)
                    Datatable2Text(dt, strFolder + "\282787_AP_SLS_" + CStr(Now.Date.ToString("yyMMdd")) + CStr("01") + ".txt")
                End If

                MessageBox.Show("Export Completed!")
            End If
        Catch ex As Exception
            MessageBox.Show("button export::" + ex.ToString)
        End Try
    End Sub
    Private Sub Datatable2Text(dt As DataTable, FilePath As String)
        Try
            Dim str As New StringBuilder()
            For i As Integer = 0 To dt.Rows.Count - 1
                For j As Integer = 0 To dt.Columns.Count - 1
                    str.Append(dt.Rows(i)(j).ToString())
                Next
                str.Append(vbCrLf)
            Next
            Dim oWrite As IO.StreamWriter
            If IO.File.Exists(FilePath) Then
                oWrite = IO.File.AppendText(FilePath)
            Else
                oWrite = IO.File.CreateText(FilePath)
            End If
            oWrite.Write(str)
            oWrite.Close()
        Catch ex As Exception
            MessageBox.Show("Datatable2Text: " + ex.ToString())
        End Try
    End Sub
End Class